package cn.yz.controller;

import cn.yz.dto.LoginDto;
import cn.yz.dto.UserDto;
import cn.yz.dto.UserPageDto;
import cn.yz.result.Result;
import cn.yz.service.UserService;
import cn.yz.vo.LoginVo;
import cn.yz.vo.PageVo;
import cn.yz.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关接口")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/page")
    @Operation(summary = "分页查询用户列表", description = "分页查询用户列表")
    public Result<PageVo<UserVo>> getUserPage(@Valid @RequestBody UserPageDto userPageDto) {
        PageVo<UserVo> pageVo = userService.getUserPage(userPageDto);
        return Result.success(pageVo);
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户列表", description = "分页获取用户列表")
    public Result<List<UserVo>> getUserList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "手机号") @RequestParam(required = false) String phone,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        List<UserVo> users = userService.getUserList(page, size, username, phone, status);
        return Result.success(users);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取用户详情", description = "根据用户ID获取用户详细信息")
    public Result<UserVo> getUserById(@Parameter(description = "用户ID") @PathVariable String id) {
        UserVo user = userService.getUserById(id);
        return Result.success(user);
    }

    @PostMapping
    @Operation(summary = "创建用户", description = "创建新用户")
    public Result<Void> createUser(@Valid @RequestBody UserDto userDto) {
        userService.createUser(userDto);
        return Result.success("创建成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "更新用户信息")
    public Result<Void> updateUser(@Parameter(description = "用户ID") @PathVariable String id,
                                 @Valid @RequestBody UserDto userDto) {
        userService.updateUser(id, userDto);
        return Result.success("更新成功");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "删除指定用户")
    public Result<Void> deleteUser(@Parameter(description = "用户ID") @PathVariable String id) {
        userService.deleteUser(id);
        return Result.success("删除成功");
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口")
    public Result<Void> register(@Valid @RequestBody UserDto userDto) {
        userService.register(userDto);
        return Result.success("注册成功");
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public Result<LoginVo> login(@Valid @RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto);
        return Result.success("登录成功", loginVo);
    }

    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出接口")
    public Result<Void> logout(@RequestHeader("Authorization") String token) {
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        userService.logout(token);
        return Result.success("登出成功");
    }

    @PostMapping("/refresh")
    @Operation(summary = "刷新令牌", description = "刷新访问令牌接口")
    public Result<LoginVo> refreshToken(@RequestParam String refreshToken) {
        LoginVo loginVo = userService.refreshToken(refreshToken);
        return Result.success("刷新成功", loginVo);
    }

    @GetMapping("/info")
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户信息")
    public Result<UserVo> getCurrentUser() {
        UserVo userVo = userService.getCurrentUser();
        return Result.success(userVo);
    }

    @PutMapping("/info")
    @Operation(summary = "更新用户信息", description = "更新当前用户信息")
    public Result<Void> updateUserInfo(@Valid @RequestBody UserDto userDto) {
        userService.updateUserInfo(userDto);
        return Result.success("更新成功");
    }

    @PostMapping("/change-password")
    @Operation(summary = "修改密码", description = "修改当前用户密码")
    public Result<Void> changePassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        userService.changePassword(oldPassword, newPassword);
        return Result.success("密码修改成功");
    }

    @PostMapping("/reset-password")
    @Operation(summary = "重置密码", description = "重置指定用户密码")
    public Result<Void> resetPassword(@RequestParam String userId, @RequestParam String newPassword) {
        userService.resetPassword(userId, newPassword);
        return Result.success("密码重置成功");
    }
} 